const mysql = require('mysql')
const dbConfig = require('./db')
const sqlMap = require('./sqlMap')

const pool = mysql.createPool({
  host: dbConfig.mysql.host,
  user: dbConfig.mysql.user,
  password: dbConfig.mysql.password,
  database: dbConfig.mysql.database,
  port: dbConfig.mysql.port,
  multipleStatements: true    // 多语句查询
})

module.exports = {
  login (req, res, next) {
    var name = req.query.name
    var password = req.query.password
    pool.getConnection((err, connection) => {
      console.log('coming login');
      var sql = sqlMap.login
      connection.query(sql, [name, password], (err, result) => {
        res.json(result)
        connection.release()
      })
    })
  },

  register (req, res, next) {
    console.log(req.body)
    var account = req.body.name
    var password = req.body.password
    var email = req.body.email
    var role = req.body.role
    pool.getConnection((err, connection) => {
      console.log("coming register");
      var sql = sqlMap.register
      connection.query(sql, [account, password, email, role], (err, result) => {
        res.json(result)
        connection.release()
      })
    })
  }
}
